package com.aj.activity.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.activity.view.PersonalAlbumGridView;
import com.aj.databases.entry.AlbumPhotoInfo;
import com.aj.databases.entry.PhotoDateInfo;
import com.aj.databases.entry.PhotoInfo;
import com.aj.utils.Contants;

public class PersonalAlbumAdapter extends BaseAdapter {

	private Context context;

	private List<PhotoDateInfo> photoDateInfoList;

	public static List<PhotoInfo> choosePhotoList;

	private boolean isManageFlag = false;

	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	public PersonalAlbumAdapter(Context context,
			List<PhotoDateInfo> photoDateInfoList) {
		choosePhotoList = new ArrayList<PhotoInfo>();
		this.context = context;
		this.photoDateInfoList = photoDateInfoList;
	}

	@Override
	public int getCount() {
		return photoDateInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		return photoDateInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHoler {
		TextView datetime;
		CheckBox checkbox_all;
		PersonalAlbumGridView gridbody;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		final PhotoDateInfo photoDateInfo = photoDateInfoList.get(position);
		ViewHoler viewHoler = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_person_album, null);
			viewHoler = new ViewHoler();
			viewHoler.datetime = (TextView) convertView
					.findViewById(R.id.datetime);
			viewHoler.gridbody = (PersonalAlbumGridView) convertView
					.findViewById(R.id.gridbody);
			viewHoler.checkbox_all = (CheckBox) convertView
					.findViewById(R.id.checkbox_all);

			convertView.setTag(viewHoler);
			PersonalAlbumPhotoAdapter adapter = new PersonalAlbumPhotoAdapter(
					context, photoDateInfo.getPhotoInfoList());
			viewHoler.gridbody.setAdapter(adapter);
			viewHoler.datetime.setText(photoDateInfo.getDate());
			viewCache.put(position, convertView);
		} else {
			viewHoler = (ViewHoler) convertView.getTag();
		}
		if (isManageFlag) {
			viewHoler.checkbox_all.setVisibility(View.VISIBLE);
			final PersonalAlbumPhotoAdapter personalAlbumPhotoAdapter = ((PersonalAlbumPhotoAdapter) viewHoler.gridbody
					.getAdapter());
			viewHoler.gridbody
					.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							PersonalAlbumPhotoAdapter.ViewHolder personAlbumViewHolder = (PersonalAlbumPhotoAdapter.ViewHolder) view
									.getTag();
							personAlbumViewHolder.checkbox_photo
									.setChecked(!personAlbumViewHolder.checkbox_photo
											.isChecked());
						}
					});
			personalAlbumPhotoAdapter.setManageFlag(isManageFlag);
			viewHoler.checkbox_all
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							personalAlbumPhotoAdapter.setCheckFlag(isChecked);
						}
					});
		} else {
			viewHoler.checkbox_all.setVisibility(View.GONE);
			((PersonalAlbumPhotoAdapter) viewHoler.gridbody.getAdapter())
					.setManageFlag(isManageFlag);
			viewHoler.gridbody
					.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							if (!isManageFlag) {
								List<PhotoInfo> photoInfoList = photoDateInfo
										.getPhotoInfoList();
								if (photoInfoList != null
										&& photoInfoList.size() > 0) {
									List<AlbumPhotoInfo> albumPhotoInfoList = new ArrayList<AlbumPhotoInfo>();
									for (PhotoInfo photoInfo : photoInfoList) {
										AlbumPhotoInfo albumPhotoInfo = new AlbumPhotoInfo();
										albumPhotoInfo.setAlbumId(photoInfo
												.getPhotoId());
										albumPhotoInfo.setAlbumUrl(photoInfo
												.getPhoto());
										albumPhotoInfoList.add(albumPhotoInfo);
									}
									Intent intent = new Intent(
											Contants.PHOTO_INFO_ACTION);
									intent.putExtra("albumList",
											(Serializable) albumPhotoInfoList);
									intent.putExtra("index", position);
									context.startActivity(intent);
								}
							}
						}
					});
		}
		return convertView;
	}

	public void setManageFlag(boolean isManage) {
		isManageFlag = isManage;
		notifyDataSetChanged();
	}

}
